package com.ruoyi.project.system.menu.service;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.TreeUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.menu.domain.Menu;
import com.ruoyi.project.system.menu.mapper.MenuMapper;
import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.role.mapper.RoleMenuMapper;

/**
 * 菜单 业务层处理
 * 
 * @author flyfish
 */
@Service("menuService")
public class MenuServiceImpl implements IMenuService {
	public static final String PREMISSION_STRING = "perms[\"{0}\"]";

	@Autowired
	private MenuMapper menuMapper;

	@Autowired
	private RoleMenuMapper roleMenuMapper;

	/**
	 * 根据用户ID查询菜单
	 * 
	 * @param userId 用户ID
	 * @return 菜单列表
	 */
	@Override
	public List<Menu> selectMenusByUserId(Long userId) {
		List<Menu> menus = menuMapper.selectMenusByUserId(userId);
		return TreeUtils.getChildPerms(menus, 0);
	}

	/**
	 * 查询菜单集合
	 * 
	 * @return 所有菜单信息
	 */
	@Override
	public List<Menu> selectMenuAll() {
		return menuMapper.selectMenuAll();
	}

	/**
	 * 根据用户ID查询权限
	 * 
	 * @param userId 用户ID
	 * @return 权限列表
	 */
	@Override
	public Set<String> selectPermsByUserId(Long userId) {
		List<String> perms = menuMapper.selectPermsByUserId(userId);
		Set<String> permsSet = new HashSet<>();
		for (String perm : perms) {
			if (StringUtils.isNotEmpty(perm)) {
				permsSet.addAll(Arrays.asList(perm.trim().split(",")));
			}
		}
		return permsSet;
	}

	/**
	 * 根据角色ID查询菜单
	 * 
	 * @param role 角色对象
	 * @return 菜单列表
	 */
	@Override
	public List<Map<String, Object>> roleMenuTreeData(Role role) {
		Long roleId = role.getRoleId();
		List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
		List<Menu> menuList = menuMapper.selectMenuAll();
		if (StringUtils.isNotNull(roleId)) {
			List<String> roleMenuList = menuMapper.selectMenuTree(roleId);
			trees = getTrees(menuList, true, roleMenuList, true);
		} else {
			trees = getTrees(menuList, false, null, true);
		}
		return trees;
	}

	/**
	 * 查询所有菜单
	 * 
	 * @param role 角色对象
	 * @return 菜单列表
	 */
	@Override
	public List<Map<String, Object>> menuTreeData() {
		List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
		List<Menu> menuList = menuMapper.selectMenuAll();
		trees = getTrees(menuList, false, null, false);
		return trees;
	}

	/**
	 * 查询系统所有权限
	 * 
	 * @return 权限列表
	 */
	@Override
	public LinkedHashMap<String, String> selectPermsAll() {
		LinkedHashMap<String, String> section = new LinkedHashMap<>();
		List<Menu> permissions = menuMapper.selectMenuAll();
		if (StringUtils.isNotEmpty(permissions)) {
			for (Menu menu : permissions) {
				section.put(menu.getUrl(), MessageFormat.format(PREMISSION_STRING, menu.getPerms()));
			}
		}
		return section;
	}

	/**
	 * 对象转菜单树
	 * 
	 * @param menuList 菜单列表
	 * @param isCheck 是否需要选中
	 * @param roleMenuList 角色已存在菜单列表
	 * @param permsFlag 是否需要显示权限标识
	 * @return
	 */
	public List<Map<String, Object>> getTrees(List<Menu> menuList, boolean isCheck, List<String> roleMenuList, boolean permsFlag) {
		List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
		for (Menu menu : menuList) {
			Map<String, Object> deptMap = new HashMap<String, Object>();
			deptMap.put("id", menu.getMenuId());
			deptMap.put("pId", menu.getParentId());
			deptMap.put("name", transMenuName(menu, roleMenuList, permsFlag));
			deptMap.put("title", menu.getMenuName());
			if (isCheck) {
				deptMap.put("checked", roleMenuList.contains(menu.getMenuId() + menu.getPerms()));
			} else {
				deptMap.put("checked", false);
			}
			trees.add(deptMap);
		}
		return trees;
	}

	public String transMenuName(Menu menu, List<String> roleMenuList, boolean permsFlag) {
		StringBuffer sb = new StringBuffer();
		sb.append(menu.getMenuName());
		if (permsFlag) {
			sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;" + menu.getPerms() + "</font>");
		}
		return sb.toString();
	}

	/**
	 * 删除菜单管理信息
	 * 
	 * @param menuId 菜单ID
	 * @return 结果
	 */
	@Override
	public int deleteMenuById(Long menuId) {
		ShiroUtils.clearCachedAuthorizationInfo();
		return menuMapper.deleteMenuById(menuId);
	}

	/**
	 * 根据菜单ID查询信息
	 * 
	 * @param menuId 菜单ID
	 * @return 菜单信息
	 */
	@Override
	public Menu selectMenuById(Long menuId) {
		return menuMapper.selectMenuById(menuId);
	}

	/**
	 * 查询子菜单数量
	 * 
	 * @param menuId 菜单ID
	 * @return 结果
	 */
	@Override
	public int selectCountMenuByParentId(Long parentId) {
		return menuMapper.selectCountMenuByParentId(parentId);
	}

	/**
	 * 查询菜单使用数量
	 * 
	 * @param menuId 菜单ID
	 * @return 结果
	 */
	@Override
	public int selectCountRoleMenuByMenuId(Long menuId) {
		return roleMenuMapper.selectCountRoleMenuByMenuId(menuId);
	}

	/**
	 * 保存菜单信息
	 * 
	 * @param menu 菜单信息
	 * @return 结果
	 */
	@Override
	public int saveMenu(Menu menu) {
		Long menuId = menu.getMenuId();
		if (StringUtils.isNotNull(menuId)) {
			menu.setUpdateBy(ShiroUtils.getLoginName());
			ShiroUtils.clearCachedAuthorizationInfo();
			return menuMapper.updateMenu(menu);
		} else {
			menu.setCreateBy(ShiroUtils.getLoginName());
			ShiroUtils.clearCachedAuthorizationInfo();
			return menuMapper.insertMenu(menu);
		}
	}

	/**
	 * 校验菜单名称是否唯一
	 * 
	 * @param menu 菜单信息
	 * @return 结果
	 */
	@Override
	public String checkMenuNameUnique(Menu menu) {
		if (menu.getMenuId() == null) {
			menu.setMenuId(-1L);
		}
		Long menuId = menu.getMenuId();
		Menu info = menuMapper.checkMenuNameUnique(menu.getMenuName());
		if (StringUtils.isNotNull(info) && StringUtils.isNotNull(info.getMenuId()) && info.getMenuId().longValue() != menuId.longValue()) {
			return UserConstants.MENU_NAME_NOT_UNIQUE;
		}
		return UserConstants.MENU_NAME_UNIQUE;
	}

}
